package com.woniuxy.java102vuecli.controller;

import com.woniuxy.java102vuecli.entity.Employee;
import com.woniuxy.java102vuecli.entity.EmployeeDeleteBatchVO;
import com.woniuxy.java102vuecli.entity.PageBean;
import com.woniuxy.java102vuecli.exception.VerifyCodeErrorException;
import com.woniuxy.java102vuecli.service.EmployeeService;
import com.woniuxy.java102vuecli.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Author author
 * @Create 2023-09-14 12:01
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @RequestMapping("/get/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Employee>> getEmployee(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pageSize") int pageSize,
            @RequestBody Employee employee){
        return new ResponseResult<PageBean<Employee>>(200,"success",
                employeeService.getEmployees(pageIndex,pageSize,employee));
    }
    @RequestMapping("/add")
    public ResponseResult addEmployee(/*@Validated(EmployeeAddGroup.class)*/ @RequestBody Employee employee/*, BindingResult bindingResult*/){
        //调用insert完成注册
        log.info("添加employee={}",employee);
//        employeeService.addEmployee(employee);
        return new ResponseResult<Void>(200,"success",null);
    }
    @RequestMapping("/modify")
    public ResponseResult modifyEmployee(/*@Validated(EmployeeModifyGroup.class)*/ @RequestBody Employee employee/*, BindingResult bindingResult*/){
        log.info("修改employee={}",employee);
        //service---dao
        return new ResponseResult<Void>(200,"success",null);
    }
    @RequestMapping("/email/{email}")
    public ResponseResult<Boolean> emailExists(@PathVariable("email")String email){
        return new ResponseResult<Boolean>(200,"success",employeeService.emailExists(email));
    }
    @RequestMapping("/remove/{eid}")
    public ResponseResult<Void> removeEmployee(@PathVariable("eid")int eid){
        log.info("删除={}",eid);
        //service,dao   update employeee set isdeleted=1 where eid=#{eid}
        return new ResponseResult<Void>(200,"success",null);
    }
    @RequestMapping("/removebatch")
    public ResponseResult<Void> removeEmployeesBatch(int[] eids){
        log.info("批量删除={}", Arrays.toString(eids));
        return new ResponseResult<Void>(200,"success",null);
    }
    @RequestMapping("/removebatch2")
    public ResponseResult<Void> removeEmployeesBatch2(@RequestBody EmployeeDeleteBatchVO employeeDeleteBatchVO){
        log.info("批量删除={}", Arrays.toString(employeeDeleteBatchVO.getEids()));
        return new ResponseResult<Void>(200,"success",null);
    }

    @RequestMapping("/login")
    public ResponseResult<String> login(String ename, String password,String uuid,String verifycode, HttpServletResponse response){
        //验证码校验
        if(!verifycode.equals(redisTemplate.opsForValue().get(uuid))){
            //验证失败
            throw new VerifyCodeErrorException("验证码错误");
        }else{
            //验证通过
            redisTemplate.delete(uuid);
        }

        Employee employee = employeeService.login(ename, password);
        //发凭证,生成token
        String token = JwtUtil.createToken(employee.getEid().toString(), employee.getEname());
        String refresh = JwtUtil.createRereshToken(employee.getEid().toString(), employee.getEname());
        //token存储请求头
        response.setHeader("token", token);
        response.setHeader("refresh", refresh);
        //前后端分离程序，后端必须暴露响应头，前端才能获取
//        response.setHeader("Access-Control-Expose-Headers", "token,refresh");
//        response.setHeader("Access-Control-Expose-Headers", "*");
        //记录Session
        return new ResponseResult<String>(200,"success","login-ok");
    }

}
